﻿using System;
using System.Reflection;
using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(USEventBase))]
public class USEventBaseEditor : Editor
{
    public static GUIStyle defaultBackground = GUIStyle.none;
    private Rect myArea = new Rect();
    private object targetEvent;

    public void DrawDefaultBox(Rect myArea, USEventBase thisEvent)
    {
        Color color = GUI.color;
        if (USSelection.IsInUnitySelection(thisEvent))
        {
            GUI.color = Color.yellow;
        }
        GUI.Box(myArea, string.Empty);
        GUI.color = color;
    }

    public Rect DrawEventInTimeline(object thisObject, Rect renderingArea)
    {
        if (defaultBackground == GUIStyle.none)
        {
            GUISkin uSeqSkin = USEditorUtility.USeqSkin;
            if (uSeqSkin != null)
            {
                defaultBackground = uSeqSkin.GetStyle("Label");
            }
        }
        USEventBase eventBase = thisObject as USEventBase;
        renderingArea.x = USControl.convertTimeToEventPanePosition(eventBase.Firetime);
        renderingArea.x = renderingArea.x;
        renderingArea.y++;
        renderingArea.width = USPreferenceWindow.DefaultEventEditorWidth;
        renderingArea.height -= 3f;
        USEventBaseEditor key = EditorEventObjectMap.GetKey(eventBase);
        if ((key != null) && (key.GetType().GetMethod("RenderEvent") != null))
        {
            try
            {
                object[] parameters = new object[] { renderingArea, eventBase };
                renderingArea = (Rect) key.GetType().GetMethod("RenderEvent").Invoke(key, parameters);
            }
            catch (TargetInvocationException exception)
            {
                Debug.Log("Caught Invocation Exception : " + exception);
            }
            catch (Exception exception2)
            {
                Debug.Log("Caught Exception : " + exception2);
            }
        }
        return renderingArea;
    }

    public Rect RenderEvent(Rect myArea, USEventBase thisEvent)
    {
        this.DrawDefaultBox(myArea, thisEvent);
        GUILayout.BeginArea(myArea);
        GUILayout.Label(thisEvent.name, defaultBackground, new GUILayoutOption[0]);
        GUILayout.EndArea();
        return myArea;
    }

    public Rect MyArea
    {
        get
        {
            return this.myArea;
        }
        set
        {
            this.myArea = value;
        }
    }

    public object TargetEvent
    {
        get
        {
            return this.targetEvent;
        }
        set
        {
            this.targetEvent = value;
        }
    }
}

